package unseen.labs.games.gamecontroller.ui.component;

import unseen.labs.games.gamecontroller.view.GenericView;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceView;

public class Joystick extends SurfaceView {

	private Context mContext;
	private GenericView mSurfaceView;

	private float mPreviousX;
	private float mPreviousY;
	private float mX;
	private float mY;
	
	public Joystick(Context context) {
		super(context);
		mContext = context;
		initView();
	}
	
	public Joystick(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		initView();
	}

	public Joystick(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		initView();
	}	

	private void initView() {
		mPreviousX 	= 0;
		mPreviousY	= 0;
		mX 			= 0;
		mY 			= 0;		
		
	}
	
	public void setSurfaceView(GenericView view){
		mSurfaceView = view;
	}
	
	@Override
	public boolean onTouchEvent(final MotionEvent event) {		
		
        if (event.getAction() == MotionEvent.ACTION_MOVE) {

        	mSurfaceView.queueEvent(new Runnable() {
                public void run() {
                	mSurfaceView.getRenderer().moveBall(mX, mY, mPreviousX, mPreviousY);   
                	mX = event.getRawX();
                    mY = event.getRawY();                	             	
                }
            });
        	
        	mPreviousX = event.getRawX();
        	mPreviousY = event.getRawY();
        }
        return true;
    }

	@Override
	public boolean onTrackballEvent(MotionEvent ev) {
		return false;
	}
	
}
